<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{lib/layui-v2.5.5/css/layui.css}" media="all">
    <link rel="stylesheet" th:href="@{css/public.css}" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <div class="demoTable">
            文件名
            <div class="layui-inline">
                <input class="layui-input" name="oldFileName" id="oldFileName" autocomplete="off">
            </div>
            id
            <div class="layui-inline">
                <input class="layui-input" name="id" id="id" autocomplete="off">
            </div>
            <button class="layui-btn" data-type="reload">根据文件名或id搜索</button>
        </div>

        <script type="text/html" id="toolbarDemo">

            <div class="layui-btn-container">
                <button type="button" class="layui-btn" name="aaa" id="test3"><i class="layui-icon"></i>上传文件</button>
                <button class="layui-btn layui-btn-sm layui-btn-danger data-delete-btn" lay-event="delete"> 删除</button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter">

        </table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-download" lay-event="download">下载</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>

    </div>
</div>
<script th:src="@{lib/layui-v2.5.5/layui.js}" charset="utf-8"></script>

<script>
    layui.use(['form', 'table', 'upload'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            upload = layui.upload;

        table.render({
            elem: '#currentTableId',
            url: 'file/queryFileInfo',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [
                [
                    {type: "checkbox", width: 50},
                    {field: 'id', width: 80, title: 'ID', sort: true},
                    {field: 'uname', width: 100, title: '用户'},
                    {field: 'oldFileName', width: 150, title: '文件名称'},
                    {field: 'ext', width: 100, title: '文件后缀'},
                    {field: 'path', width: 150, title: '存储路径'},
                    {field: 'size', width: 100, title: '文件大小'},
                    {field: 'type', title: '类型', Width: 80},
                    {field: 'downcounts', width: 100, title: '下载次数'},
                    {field: 'uploadTime', width: 150, title: '上传时间'},

                    {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
                ]
            ],
            limits: [10, 15, 20, 25, 50, 100],
            limit: 10,
            page: true,
            skin: 'line',
            id: 'testReload'
        });

        var $ = layui.$, active = {
            reload: function () {
                //var demoReload = $('#demoReload');
                var oldFileNameId = $('#oldFileName');
                var idId = $('#id');
                //执行重载
                table.reload('testReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        oldFileName: oldFileNameId.val().trim(),//trim去空格的
                        id: idId.val().trimEnd()
                    }
                }, 'data');
            }
        };

        $('.demoTable .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        //上传文件
        upload.render({
            elem: '#test3'
            , url: 'file/uploadFile' //改成您自己的上传接口
            , accept: 'file' //普通文件
            , done: function (res) {
                if (res.code == 200) {
                    layer.msg("上传成功", {
                        icon: 6,
                        time: 500,
                    }, function () {
                        parent.window.location.reload();//重新页面
                        var ifremeIndex = parent.layer.getFrameIndex(window.name);
                        parent.layer.close(ifremeIndex);
                    })
                } else {
                    layer.msg("上传失败");
                }
            }
        });
        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            // 监听添加操作
            var data = obj.data;
            if (obj.event === 'download') {
                window.open("file/download/" + data.id);
                obj.update({
                    "downcounts": data.downcounts + 1
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                // var checkStatus = table.checkStatus('currentTableId')
                //     , data = checkStatus.data;
                // layer.alert(JSON.stringify(data));

                //选中信息
                var checkStatus = table.checkStatus(obj.config.id);
                var data = checkStatus.data;
                console.log(data);
                if (data.length == 0) {
                    layer.msg("请选择要删除的记录信息");
                } else {//删除工作
                    //获取ids
                    var ids = getCheckId(data);
                    console.log(ids);
                    layer.confirm('你真的要删除吗？', function (index) {
                        deleteFileByIds(ids, index);
                    })
                }
            }
        });

        //获取选中id的集合
        function getCheckId(data) {
            var arr = new Array();
            for (var i = 0; i < data.length; i++) {
                arr.push(data[i].id);
            }
            return arr.join(",");
        }


        /**
         *根据id删除记录信息
         */
        function deleteFileByIds(ids, index) {
            $.ajax({
                url: "file/delete",
                type: "POST",
                data: {'ids': ids},
                success: function (result) {
                    if (result.code == 200) {
                        layer.msg("删除成功", {
                            icon: 6,
                            time: 500,
                        }, function () {
                            parent.window.location.reload();//重新页面
                            var ifremeIndex = parent.layer.getFrameIndex(window.name);
                            parent.layer.close(ifremeIndex);
                        })
                    } else {
                        layer.msg("删除失败");
                    }
                }
            })
            return false;
        };

        //监听表格复选框选择
        table.on('checkbox(currentTableFilter)', function (obj) {
            console.log(obj)
        });

        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'download') {
                window.open("file/download/" + data.id);
                obj.update({
                    "downcounts": data.downcounts + 1
                });
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    //调用删除函数实现删除工作
                    deleteFileByIds(data.id, index);
                });
            }
        });

    });
</script>

</body>
</html>